package com.lfs.admin.controller.dataPackage;


import com.lfs.admin.model.entity.dataPackage.DataUploadDetailEntity;
import com.lfs.admin.model.entity.dataPackage.DataUploadEntity;
import com.lfs.admin.model.vo.dataPackage.DataPackageVo;
import com.lfs.admin.model.vo.dataPackage.DataUploadVo;
import com.lfs.admin.service.DataPackageService;
import com.lfs.admin.service.MinioService;
import com.lfs.base.dto.Result;
import com.lfs.base.dto.ResultObject;
import com.lfs.base.exception.BusinessException;
import com.lfs.base.util.StringUtils;
import com.lfs.common.annotation.Log;
import com.lfs.common.core.controller.BaseController;
import com.lfs.common.core.domain.AjaxResult;
import com.lfs.common.core.page.TableDataInfo;
import com.lfs.common.enums.BusinessType;
import com.lfs.common.utils.file.FileUploadUtils;
import com.lfs.dao.entity.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

@RestController
@RequestMapping("/dataPackage")
@Api(value = "数据包上传管理", tags = {"数据包上传管理"})
public class DataPackageController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(DataPackageController.class);

    @Value("${lfs.profile:/data/upload}")
    private String uploadPath;

    @Autowired
    private DataPackageService dataPackageService;

    @Autowired
    private MinioService minioService;

    /**
     * 数据包上传
     */
    @Log(title = "数据包上传", businessType = BusinessType.INSERT)
    @ApiOperation(value = "数据包上传")
    @PostMapping(value = "/upload")
    public AjaxResult dataUpload(@RequestPart("uploadFile") MultipartFile file) throws IOException {
        String url = "";
        if (!file.isEmpty()) {
            try {
                url = minioService.uploadFile(file, "data");
            } catch (Exception e) {
                logger.error("数据包上传失败,异常={}",e.getMessage(),e);
            }
            AjaxResult ajax = AjaxResult.success();
            ajax.put("imgUrl", url);
            // 上传成功返回url
            return ajax;
        }
        return AjaxResult.error("请选择需要上传的文件!");
    }

    @Log(title = "数据包解析", businessType = BusinessType.INSERT)
    @ApiOperation(value = "数据包解析")
    @PostMapping("/analysis")
    public AjaxResult analysis(@RequestBody DataPackageVo dataPackageVo) {

        if (null == dataPackageVo) {
            return AjaxResult.error("需要解析的数据包数据为空!");
        }
        String result = dataPackageService.analysisData(dataPackageVo);
        return AjaxResult.success(result);
    }

    @Log(title = "数据包上传记录", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('dataUpload:list')")
    @ApiOperation(value = "数据包上传记录")
    @PostMapping("/list")
    public Result<?> list(@RequestBody DataUploadVo dataUploadVo) {

        if (null == dataUploadVo) {
            throw new BusinessException("查询数据包上传记录接口参数不能为空!");
        }

        logger.info("根据{},查询数据包上传记录请求参数", dataUploadVo.toString());
        PageBean<DataUploadEntity> dataUploadEntityList = dataPackageService.queryDataUploadList(dataUploadVo);
        return ResultObject.successObject(dataUploadEntityList,"success");
    }

    /**
     * 根据数据包id获取解析详细信息
     */
    @PreAuthorize("@ss.hasPermi('dataUpload:view')")
    @PostMapping(value = {"/getUploadDetail"})
    @ApiOperation(value = "查询数据包详情接口")
    public Result<?> getUploadDetail(@RequestBody DataUploadVo dataUploadVo) {
        PageBean<DataUploadDetailEntity> dataUploadDetailEntityList = dataPackageService.getDataUploadDetailList(dataUploadVo);
        return ResultObject.successObject(dataUploadDetailEntityList,"success");
    }

    @ApiOperation(value = "删除数据包记录")
    @DeleteMapping("/deleteData")
    public Result<?> deleteData(@PathVariable Integer[] ids) {
        if (null == ids) {
            throw new BusinessException("删除数据包记录接口参数不能为空!");
        }
        logger.info("删除数据包记录请求");
        return ResultObject.successObject(dataPackageService.deleteData(ids), "删除数据包记录成功");
    }
}
